﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Blazing.WebFacade.Controls
{
    public class CustomTreeNode : TreeNode, IComparable
    {    
        private string _cssClass;
        public string CssClass
        {
            get
            {
                return _cssClass;
            }
            set
            {
                _cssClass = value;
            }
        }

        private int _score;
        public int Score
        {
            get
            {
                return _score;
            }
            set
            {
                _score = value;
            }
        }   

        public CustomTreeNode()
        {
            
        }

        public CustomTreeNode(string text, string cssClass)
        {
            this.Text = text;
            this.CssClass = cssClass;
        }

        public CustomTreeNode(string text, string cssClass, string value)
        {
            this.Text = text;
            this.CssClass = cssClass;
            this.Value = value;
        }

        protected override void RenderPreText(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, _cssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            base.RenderPreText(writer);
        }

        protected override void RenderPostText(HtmlTextWriter writer)
        {
            writer.RenderEndTag();
            base.RenderPostText(writer);
        }

        // Search by score, then by name/text (alphabetically)
        public int CompareTo(object nodeToCompare)
        {
            if (nodeToCompare is CustomTreeNode)
            {
                CustomTreeNode node = (CustomTreeNode)nodeToCompare;
                if (Score > node.Score)
                    return -1;
                else if (Score < node.Score)
                    return 1;
                else
                {
                    return this.Text.CompareTo(node.Text);
                }
            }
            else
            {
                return 0;
            }
        }
    }
}
